package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.mapper.EduVideoMapper;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author faiz
 * @since 2021-04-21
 */
@Slf4j
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
    //注入vodclient
    @Autowired
    private VodClient vodClient;

    //TODO 删除小节，还要删除对应的视频文件
    @Override
    public void removeVideoByCourseId(String courseId) {
//        System.out.println("调用测试！！！");
        QueryWrapper<EduVideo> deleteWrapper = new QueryWrapper<>();
        deleteWrapper.eq("course_id", courseId);
        List<EduVideo> videoList = list(deleteWrapper);
        List<String> videoIdList = new ArrayList<>();
        for (EduVideo eduVideo : videoList) {
            if (!StringUtils.isEmpty(eduVideo.getVideoSourceId())) {
                videoIdList.add(eduVideo.getVideoSourceId());
            }
        }
        System.out.println(videoIdList.size());
        videoIdList.stream().forEach(System.out::println);
        vodClient.deleteBatch(videoIdList);
//        vodClient.deleteBatch(
////          videoList.stream().filter(video -> !StringUtils.isEmpty(video.getVideoSourceId()))
////                .map(EduVideo::getVideoSourceId).collect(Collectors.toList())
//        );
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        remove(wrapper);
    }
}
